<template>
    <div class="main_box" style="padding:10px">
      <div class="add_wrap">
        <div class="tit">
          <h2>{{dataForm.factoryId?'编辑厂家':'新增厂家'}}</h2>
          <el-button type="success" plain size="small" icon="fa fa-mail-forward" @click="returnList">返回</el-button>
        </div>
        <el-form :model="dataForm" ref="dataForm" label-width="200px" :rules="roleRules">
          <el-row>
            <el-form-item prop="factoryName" label="厂家名称" >
              <el-input type="text" v-model="dataForm.factoryName" clearable placeholder="请输入厂家名称"></el-input>
            </el-form-item>
            <!-- <el-form-item prop="factoryType" label="厂家类型">
              <el-select v-model="dataForm.factoryType" placeholder="请选择厂家类型">
                <el-option v-for="item in factoryTypeList" :value="item.id" :label="item.name" :key="item.id"></el-option>
              </el-select>
            </el-form-item> -->
            <el-form-item prop="linkMan" label="联系人">
              <el-input type="text" v-model="dataForm.linkMan" placeholder="请输入联系人"></el-input>
            </el-form-item>
            <el-form-item prop="linkPhone" label="联系电话">
              <el-input type="number" v-model="dataForm.linkPhone" placeholder="请输入联系电话"></el-input>
            </el-form-item>
            <el-form-item prop="linkUrl" label="厂家链接">
              <el-input type="text" v-model="dataForm.linkUrl" placeholder="请输入厂家链接"></el-input>
            </el-form-item>
            <el-form-item prop="address" label="厂家地址">
              <el-input type="text" v-model="dataForm.address" placeholder="请输入厂家地址"></el-input>
            </el-form-item>

            <el-form-item prop="remark" label="备注">
              <el-input type="textarea" v-model="dataForm.remark"></el-input>
            </el-form-item>
          </el-row>
          <el-row>
            <el-form-item>
              <el-button plain type="primary" @click="sureClick">保 存</el-button>
              <el-button plain @click="returnList">取 消</el-button>
            </el-form-item>
          </el-row>
        </el-form>
      </div>
    </div>
  </template>

  <script>
    import * as message from "@/constants/message";
    import FactoryApi from '@/api/system/factory';
    export default {
      data() {
        return {
          dataForm: {
            factoryId:"",
            factoryName:"",
            // factoryTye:"",
            linkMan:"",
            linkPhone: "",
            linkUrl:"",
            address:"",
            remark:"",
          },
          factoryTypeList:[
            {id:1,name:"第一个类型"},
            {id:2,name:"第二个类型"}
          ],
          roleRules: {
            factoryName:[
              {required:true,message:"请输入厂家名称",trigger:"focus"}
            ],
            // factoryType: [
            //   {required: true, message: "请选择厂家类型", trigger: "change"}
            // ],
            linkPhone:[
                { pattern:  /^400[0-9]{7}|^1[345789]\d{9}$|^0[0-9]{2,3}-[0-9]{8}/, message: "电话格式错误", trigger: "blur" },
            ]

          }
        }
      },
      components: {
      },
      mounted() {
        this.init()
      },
      methods: {
        init() {
          if (this.$route.query.id) {
            this.dataForm.id = this.$route.query.id
            this.getData()
          }
        },
        keydown(e) {
          if (e.keyCode == 32) {
            e.returnValue = false;
          }
        },
        getData() {
          FactoryApi.getFactory({
            id: this.dataForm.id
          }).then(res => {
            this.$Utils.bindingBean(this.dataForm, res.data)
          })
        },

        sureClick() {
          this.$refs.dataForm.validate(valid => {
            if (valid) {
              this.$confirm(message.MSG_CONFIRM_SAVE_ALTER, message.TITLE_PROMPT, {})
                .then(() => {
                  if (this.dataForm.factoryId) {
                    this.editData();
                  } else {
                    this.addData();
                  }
                });
            }
          })
        },
        addData() {
          let param = {
            factoryName: this.dataForm.factoryName,
            // factoryType: this.dataForm.factoryType,
            linkMan:this.dataForm.linkMan,
            linkUrl:this.dataForm.linkUrl,
            linkPhone:this.dataForm.linkPhone,
            address:this.dataForm.address,
            remark:this.dataForm.remark,
          };
          FactoryApi.addFactory(param).then(res => {
            this.$Utils.notifySuccess()
            this.returnList();
          })
        },
        editData() {
          let param = {
            factoryId:this.dataForm.factoryId,
            factoryName: this.dataForm.factoryName,
            // factoryType: this.dataForm.factoryType,
            linkMan:this.dataForm.linkMan,
            linkUrl:this.dataForm.linkUrl,
            linkPhone:this.dataForm.linkPhone,
            address:this.dataForm.address,
            remark:this.dataForm.remark,
          };
          FactoryApi.editFactory(param)
            .then(data => {
              this.$Utils.notifySuccess()
              this.returnList();
            })
        },
        returnList() {
          this.$store.dispatch('delView', this.$route);
          this.$router.push({path: '/organ/factoryManagement'});
        }
      },

    }
  </script>
<style lang="scss" scoped>
@import "@/assets/css/views/add.scss";
</style>

  <style lang="scss" scoped>
    .upload-img {
      &::v-deep .el-upload-dragger {
        display: flex;
        justify-content: space-around;
        align-items: center;

        .tip {
          padding-bottom: 30px;
        }
      }
    }
  </style>

